<?php
namespace Modules\Frontend\Controllers;

class UserTeamController extends \Phalcon\Mvc\Controller {
	public function beforeExecuteRoute() {
		\Components\Utils::isClosedFrontent($this);
	}

	public function initialize() {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');
	}

	public function pagerAction() {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$query = $this -> request -> getQuery();
		if(!isset($query['page'])) {
			$query['page'] = 1;
		} else if($query['page'] <= 0)  {
			$query['page'] = 1;
		} else {
			$query['page'] = abs($query['page']);
		}

		if(!isset($query['limit'])) {
			$query['limit'] = 10;
		} else if($query['limit'] <= 0)  {
			$query['limit'] = 10;
		} else {
			$query['limit'] = abs($query['limit']);
		}

		$service = $this -> account::getTeamServiceInstance() -> pager([
			'conditions' => 'status in(0,1) AND pid = ' . $serviceUser['user'] -> user_id,
			'columns' => 'user_id, account, status',
			'order' => 'status DESC, user_id DESC'
		], $query['limit'], $query['page']);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>'无商户']);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'pager' => $service['pager']]);
	}

	public function childrenAction() {
		$query = $this -> request -> getQuery();
		if(!isset($query['page'])) {
			$query['page'] = 1;
		} else if($query['page'] <= 0)  {
			$query['page'] = 1;
		} else {
			$query['page'] = abs($query['page']);
		}

		if(!isset($query['limit'])) {
			$query['limit'] = 10;
		} else if($query['limit'] <= 0)  {
			$query['limit'] = 10;
		} else {
			$query['limit'] = abs($query['limit']);
		}

		$service = $this -> account::getTeamServiceInstance() -> pager([
			'conditions' => 'status in(0,1) AND pid = ' . $query['user_id'],
			'columns' => 'user_id, account, status',
			'order' => 'status DESC, user_id DESC'
		], $query['limit'], $query['page']);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>'无商户']);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'pager' => $service['pager']]);
	}

	public function activationAction() {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$findFirstCUser = $this -> account::getServiceInstance() -> findFirstById($this -> request -> getPost('to_user_id', 'int', 0));
		if($findFirstCUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$findFirstCUser['code'],'msg'=>$findFirstCUser['message']]);
		}
		if($findFirstCUser['user'] -> pid != $serviceUser['user'] -> user_id) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'激活的账号有误']);
		}
		if($findFirstCUser['user'] -> status == 0) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'账号已激活']);
		}

		$findFirstCUser['user'] -> status = 0;

		$this -> db -> begin();
		try {
			$service = $this -> account::getWalletServiceInstance() -> updateDecAmount(['type'=>5,'amount'=>1,'user_id'=>$serviceUser['user'] -> user_id]);
			if($service['code'] != 0) {
				throw new \Exception($service['message']);
				//throw new \Exception('激活失败');
			}

			if(!$findFirstCUser['user'] -> save()) {
				throw new \Exception('激活失败');
			}
			$this -> db -> commit();
		} catch(\Exception $e) {
			$this -> db -> rollback();

			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>$e -> getMessage()]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'激活成功']);
	}

	public function tradeAction() {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$post = $this -> request -> getPost();		
		if(!$this -> security -> checkHash($post['safePassword'], $serviceUser['user'] -> safe_password)) {
			return $this -> response -> setJsonContent(['code' => -1, 'msg' => '安全密码错误']);
		}

		$findFirstCUser = $this -> account::getServiceInstance() -> findFirstById($post['to_user_id']);
		if($findFirstCUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$findFirstCUser['code'],'msg'=>$findFirstCUser['message']]);
		}
		/*
		if($findFirstCUser['user'] -> pid != $serviceUser['user'] -> user_id) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'转出的账号有误']);
		}
		*/

		if($post['amount'] <= 0) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'数量不能少于0']);
		}

		$this -> db -> begin();
		try {
			$serviceFirst = $this -> account::getWalletServiceInstance() -> updateDecAmount(['type'=>$post['field'],'amount'=>$post['amount'],'user_id'=>$serviceUser['user'] -> user_id]);
			if($serviceFirst['code'] != 0) {
				throw new \Exception($serviceFirst['message']);
			}

			$serviceSecond = $this -> account::getWalletServiceInstance() -> updateIncAmount(['type'=>$post['field'],'amount'=>$post['amount'],'user_id'=>$findFirstCUser['user'] -> user_id]);
			if($serviceSecond['code'] != 0) {
				throw new \Exception($serviceSecond['message']);;
			}

			$this -> db -> commit();
		} catch(\Exception $e) {
			$this -> db -> rollback();

			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>$e -> getMessage()]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'转出成功']);
	}
}